package facade;

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;
import org.example.ws.FacadeSkeletonInterface;
import org.example.ws.GetAllEventsFaultException;
import org.example.ws.GetAllGlobalDataFaultException;
import org.example.ws.GetAllMovieDataFault;
import org.example.ws.GetAllNewsFaultException;
import org.example.ws.GetAllRestaurantDataFaultException;
import org.example.ws.GetEventsByDateFaultException;
import org.example.ws.GetEventsByKeywordFaultException;
import org.example.ws.GetMovieInfoByGenreFault;
import org.example.ws.GetMovieInfoByNameFault;
import org.example.ws.GetMovieInfoByTheatreFault;
import org.example.ws.GetMovieInfoByTimeFault;
import org.example.ws.GetMovieInfoByTypeFault;
import org.example.ws.GetNewsByCategoryFaultException;
import org.example.ws.GetNewsByKeywordFaultException;
import org.example.ws.GetRestaurantInfoByCusineFaultException;
import org.example.ws.GetRestaurantInfoByNameFaultException;
import org.example.ws.GetRestaurantInfoByRatingFaultException;
import org.example.www.globalsearch.GetAllData;
import org.example.www.globalsearch.GetAllDataResponse;
import org.example.www.globalsearch.GlobalEventList;
import org.example.www.globalsearch.GlobalEvents;
import org.example.www.globalsearch.GlobalMoviesList;
import org.example.www.globalsearch.GlobalNewsList;
import org.example.www.globalsearch.GlobalRestaurantList;
import org.example.www.newwsdlfile.Event;
import org.example.www.newwsdlfile.EventsArray;
import org.example.www.newwsdlfile.GetAllEvents;
import org.example.www.newwsdlfile.GetAllEventsResponse;
import org.example.www.newwsdlfile.GetAllGlobalData;
import org.example.www.newwsdlfile.GetAllGlobalFacadeData;
import org.example.www.newwsdlfile.GetAllGlobalFacadeDataResponse;
import org.example.www.newwsdlfile.GetAllMovieData;
import org.example.www.newwsdlfile.GetAllMovieDataResponse;
import org.example.www.newwsdlfile.GetAllNews;
import org.example.www.newwsdlfile.GetAllNewsResponse;
import org.example.www.newwsdlfile.GetAllRestaurantData;
import org.example.www.newwsdlfile.GetAllRestaurantDataFault;
import org.example.www.newwsdlfile.GetAllRestaurantDataResponse;
import org.example.www.newwsdlfile.GetEventsByDate;
import org.example.www.newwsdlfile.GetEventsByDateResponse;
import org.example.www.newwsdlfile.GetEventsByKeyword;
import org.example.www.newwsdlfile.GetEventsByKeywordResponse;
import org.example.www.newwsdlfile.GetMovieInfoByGenre;
import org.example.www.newwsdlfile.GetMovieInfoByGenreResponse;
import org.example.www.newwsdlfile.GetMovieInfoByName;
import org.example.www.newwsdlfile.GetMovieInfoByNameResponse;
import org.example.www.newwsdlfile.GetMovieInfoByTheatre;
import org.example.www.newwsdlfile.GetMovieInfoByTheatreResponse;
import org.example.www.newwsdlfile.GetMovieInfoByTime;
import org.example.www.newwsdlfile.GetMovieInfoByTimeResponse;
import org.example.www.newwsdlfile.GetMovieInfoByType;
import org.example.www.newwsdlfile.GetMovieInfoByTypeResponse;
import org.example.www.newwsdlfile.GetNewsByCategory;
import org.example.www.newwsdlfile.GetNewsByCategoryResponse;
import org.example.www.newwsdlfile.GetNewsByKeyword;
import org.example.www.newwsdlfile.GetNewsByKeywordResponse;
import org.example.www.newwsdlfile.GetRestaurantInfoByCusine;
import org.example.www.newwsdlfile.GetRestaurantInfoByCusineFault;
import org.example.www.newwsdlfile.GetRestaurantInfoByCusineResponse;
import org.example.www.newwsdlfile.GetRestaurantInfoByName;
import org.example.www.newwsdlfile.GetRestaurantInfoByNameFault;
import org.example.www.newwsdlfile.GetRestaurantInfoByNameResponse;
import org.example.www.newwsdlfile.GetRestaurantInfoByRating;
import org.example.www.newwsdlfile.GetRestaurantInfoByRatingFault;
import org.example.www.newwsdlfile.GetRestaurantInfoByRatingResponse;
import org.example.www.newwsdlfile.News;
import org.example.www.newwsdlfile.Restaurant;
import org.example.www.newwsdlfile.Movie;
import org.example.www.newwsdlfile.Movies;
import org.example.www.newwsdlfile.MoviesList;
import org.example.www.newwsdlfile.RestaurantList;


import org.eventexample.ws.EventsDAO;
import org.eventexample.ws.EventsStub;
import org.newsexample.ws.NewsDAO;
import org.newsexample.ws.NewsStub;
import org.restaurantexample.ws.ErrorMessage;
import org.restaurantexample.ws.RestaurantDAO;
import org.restaurantexample.ws.RestaurantServiceStub;
import org.sjsuglobalsearch.www.movieservice.GetMovieByGenre;
import org.sjsuglobalsearch.www.movieservice.GetMovieByGenreResponse;
import org.sjsuglobalsearch.www.movieservice.GetMovieByTime;
import org.sjsuglobalsearch.www.movieservice.GetMovieByTimeResponse;
import org.sjsuglobalsearch.www.movieservice.MovieData;
import org.sjsuglobalsearch.www.movieservice.MovieList;

import org.movieexample.ws.GetMovieByGenreFaultException;
import org.movieexample.ws.GetMovieByTimeFaultException;
import org.movieexample.ws.MovieDAO;
import org.movieexample.ws.MovieServiceStub;

import org.globalSearchexample.ws.GetAllDataFaultException;
import org.globalSearchexample.ws.GlobalSearchStub;

import facade.ErrorMessages;
import facade.ServiceUnavailableException;



public class FacadeBusiness implements FacadeSkeletonInterface
{

	private static final String RESTAURANT_SERVICE_END_POINT = "http://localhost:8080/axis2/services/RestaurantService";
	private static final String NEWS_SERVICE_END_POINT = "http://localhost:8080/axis2/services/News";
	private static final String EVENTS_SERVICE_END_POINT = "http://localhost:8080/axis2/services/Events";
	private static final String MOVIES_SERVICE_END_POINT = "http://localhost:8080/axis2/services/MovieService";
	private static final String GLOBAL_SERVICE_END_POINT ="http://localhost:8080/axis2/services/GlobalSearch";
	private EventsStub eStub;
	private RestaurantServiceStub rStub;
	private NewsStub nStub;
	private MovieServiceStub mStub;
	private GlobalSearchStub gStub;
	public FacadeBusiness() throws AxisFault
	{
		try
		{
			eStub = new EventsStub(EVENTS_SERVICE_END_POINT);
			rStub = new RestaurantServiceStub(RESTAURANT_SERVICE_END_POINT);
			nStub = new NewsStub(NEWS_SERVICE_END_POINT);
			mStub = new MovieServiceStub(MOVIES_SERVICE_END_POINT);
			gStub = new GlobalSearchStub(GLOBAL_SERVICE_END_POINT);
		}
		catch(AxisFault e)
		{
			System.out.println(e.getMessage());
		}
	}
	@Override
	public GetEventsByKeywordResponse getEventsByKeyword(GetEventsByKeyword getEventsByKeyword) throws GetEventsByKeywordFaultException {
		
		GetEventsByKeywordResponse object = new GetEventsByKeywordResponse();
		
		String keyword = (String)getEventsByKeyword.getKeyword();
		
		EventsDAO dao = new EventsDAO();
		org.eventexample.www.events.Event[] eventsResult = dao.getEventsByKeyword(keyword);
		
		Event[] result = new Event[eventsResult.length];
		for(int i=0; i<eventsResult.length;i++)
		{
			result[i] = this.convertEvents(eventsResult[i]);
		}
		
		EventsArray array = new EventsArray();
		array.setEvent(result);
		object.setEventsArray(array);
		
		return object;
		
	}

	@Override
	public GetEventsByDateResponse getEventsByDate(GetEventsByDate getEventsByDate)throws GetEventsByDateFaultException {
		GetEventsByDateResponse object = new GetEventsByDateResponse();
		
		String startDate = (String)getEventsByDate.getStart();
		String endDate = (String)getEventsByDate.getEnd();
		
		EventsDAO dao = new EventsDAO();
		org.eventexample.www.events.Event[] eventsResult = dao.getEventsByDate(startDate, endDate);
		
		Event[] result = new Event[eventsResult.length];
		for(int i=0; i<eventsResult.length;i++)
		{
			result[i] = this.convertEvents(eventsResult[i]);
		}
		
		EventsArray array = new EventsArray();
		array.setEvent(result);
		object.setEventArray(array);
		
		return object;
	}

	@Override
	public GetNewsByKeywordResponse getNewsByKeyword(GetNewsByKeyword getNewsByKeyword) throws GetNewsByKeywordFaultException {
		GetNewsByKeywordResponse object = new GetNewsByKeywordResponse();
		
		String keyword = (String)getNewsByKeyword.getKeyword();
		
		NewsDAO dao = new NewsDAO();
		org.newsexample.www.news.News[] newsResult = dao.getNewsByKeyword(keyword);
		
		News[] result = new News[newsResult.length];
		for(int i=0; i<newsResult.length;i++)
		{
			result[i] = this.convertNews(newsResult[i]);
		}
		
		org.example.www.newwsdlfile.NewsArray array = new org.example.www.newwsdlfile.NewsArray();
		array.setNews(result);
		object.setNewsArray(array);
		
		return object;
	}

	@Override
	public GetNewsByCategoryResponse getNewsByCategory( GetNewsByCategory getNewsByCategory) throws GetNewsByCategoryFaultException {
		GetNewsByCategoryResponse object = new GetNewsByCategoryResponse();
		
		String category = (String)getNewsByCategory.getCategory();
		
		NewsDAO dao = new NewsDAO();
		org.newsexample.www.news.News[] newsResult = dao.getNewsByCategory(category);
		
		News[] result = new News[newsResult.length];
		for(int i=0; i<newsResult.length;i++)
		{
			result[i] = this.convertNews(newsResult[i]);
		}
	
		org.example.www.newwsdlfile.NewsArray array = new org.example.www.newwsdlfile.NewsArray();
		array.setNews(result);
		object.setNewsArray(array);
		
		return object;
	}

	@Override
	public GetAllEventsResponse getAllEvents(GetAllEvents getAllEvents) throws GetAllEventsFaultException {
		GetAllEventsResponse object = new GetAllEventsResponse();
		
		EventsDAO dao = new EventsDAO();
		
		org.eventexample.www.events.Event[] eventsResult = dao.getAllEvents();
		
		Event[] result = new Event[eventsResult.length];
		for(int i=0; i<eventsResult.length;i++)
		{
			result[i] = this.convertEvents(eventsResult[i]);
		}
		
		EventsArray array = new EventsArray();
		array.setEvent(result);
		object.setEventsArray(array);
		
		return object;
	}

	@Override
	public GetAllNewsResponse getAllNews(GetAllNews getAllNews) throws GetAllNewsFaultException {
		GetAllNewsResponse object = new GetAllNewsResponse();
		
		NewsDAO dao = new NewsDAO();
		
		org.newsexample.www.news.News[] newsResult = dao.getAllNews();
		
		News[] result = new News[newsResult.length];
		for(int i=0; i<newsResult.length;i++)
		{
			result[i] = this.convertNews(newsResult[i]);
		}
		
		org.example.www.newwsdlfile.NewsArray array = new org.example.www.newwsdlfile.NewsArray();
		array.setNews(result);
		object.setNewsArray(array);
		
		return object;
	}
	
	@Override
	public GetRestaurantInfoByRatingResponse getRestaurantInfoByRating(
			GetRestaurantInfoByRating getRestaurantInfoByRating)
			throws GetRestaurantInfoByRatingFaultException {
		
		try
		{
			GetRestaurantInfoByRatingResponse response = new GetRestaurantInfoByRatingResponse();
			RestaurantDAO restaurantDAO = new RestaurantDAO();
			org.sjsusearch.www.restaurantservice.RestaurantList restaurantData = restaurantDAO.getRestaurantByRating(getRestaurantInfoByRating.getIn());
			
			RestaurantList result = new RestaurantList();
			result = this.convertRestaurantLists(restaurantData);
			
			response.setOut(result);
			return response;
		}
		catch(Exception e)
		{
			GetRestaurantInfoByRatingFault ratingFault = new GetRestaurantInfoByRatingFault();
			ratingFault.setGetRestaurantInfoByRatingFault(ErrorMessage.ServiceUnavailableError);
			GetRestaurantInfoByRatingFaultException error = new GetRestaurantInfoByRatingFaultException();
			error.setFaultMessage(ratingFault);
			throw error;	
		}
	}


	@Override
	public GetRestaurantInfoByCusineResponse getRestaurantInfoByCusine(
			GetRestaurantInfoByCusine getRestaurantInfoByCusine)
			throws GetRestaurantInfoByCusineFaultException {

		try
		{
			
			GetRestaurantInfoByCusineResponse response = new GetRestaurantInfoByCusineResponse();
			org.sjsusearch.www.restaurantservice.GetRestaurantInfoByCusine parm = new org.sjsusearch.www.restaurantservice.GetRestaurantInfoByCusine();
			parm.setIn(getRestaurantInfoByCusine.getIn());
			org.sjsusearch.www.restaurantservice.GetRestaurantInfoByCusineResponse res = new org.sjsusearch.www.restaurantservice.GetRestaurantInfoByCusineResponse();
			try 
			{
				res = rStub.getRestaurantInfoByCusine(parm);
			} 
			catch (RemoteException e1) {
				e1.printStackTrace();
			} 
			catch (org.restaurantexample.ws.GetRestaurantInfoByCusineFaultException e1) {
				e1.printStackTrace();
			}
			
			org.sjsusearch.www.restaurantservice.RestaurantList restaurantData = res.getOut();
			
			RestaurantList result = new RestaurantList();
			result = this.convertRestaurantLists(restaurantData);
			
			response.setOut(result);
			return response;			
		}
		catch(Exception e)
		{
			GetRestaurantInfoByCusineFault cusineFault = new GetRestaurantInfoByCusineFault();
			cusineFault.setGetRestaurantInfoByCusineFault(ErrorMessage.ServiceUnavailableError);
			GetRestaurantInfoByCusineFaultException error = new GetRestaurantInfoByCusineFaultException();
			error.setFaultMessage(cusineFault);
			throw error;
		}
	}


	@Override
	public GetAllRestaurantDataResponse getAllRestaurantData(
			GetAllRestaurantData getAllRestaurantData)
			throws GetAllRestaurantDataFaultException {
		
		//No Need to implement: Taken care by Restaurant Data
		return null;
		
	}

	@Override
	public GetRestaurantInfoByNameResponse getRestaurantInfoByName(
			GetRestaurantInfoByName getRestaurantInfoByName)
			throws GetRestaurantInfoByNameFaultException {

		try
		{
			GetRestaurantInfoByNameResponse response = new GetRestaurantInfoByNameResponse();
			RestaurantDAO restaurantDAO = new RestaurantDAO();
			org.sjsusearch.www.restaurantservice.Restaurant restaurantData = restaurantDAO.getRestaurantByName(getRestaurantInfoByName.getIn());
			
			Restaurant result = new Restaurant();
			result = this.convertRestaurant(restaurantData);
			
			response.setOut(result);
			return response;
		}
		catch(Exception e)
		{
			GetRestaurantInfoByNameFault nameFault = new GetRestaurantInfoByNameFault();
			nameFault.setGetRestaurantInfoByNameFault(ErrorMessage.ServiceUnavailableError);
			GetRestaurantInfoByNameFaultException error = new GetRestaurantInfoByNameFaultException();
			error.setFaultMessage(nameFault);
			throw error;	
		}
	}
	
	@Override
	public GetMovieInfoByTypeResponse getMovieInfoByType(
			GetMovieInfoByType getMovieInfoByType)
			throws GetMovieInfoByTypeFault {
		
		GetMovieInfoByTypeResponse response = new GetMovieInfoByTypeResponse();
		try 
		{
			MovieDAO movieDAO = new MovieDAO();
			MovieData[] movieArray = movieDAO.getMovieInfoByType("PG").getMovieList();
			
			//Movie[] movielistConverted = this.convertMovieArray(movieArray);
			Movies movies = new Movies();
			
			//movies.setMovie(movielistConverted);
			response.setGetMovieInfoByTypeResponse(movies);
		} 
		catch (Exception e) 
		{
			
		}
		
		return response;
	}

	

	@Override
	public GetMovieInfoByTimeResponse getMovieInfoByTime(
			GetMovieInfoByTime getMovieInfoByTime)
			throws GetMovieInfoByTimeFault
	{
		return null;
	}

	@Override
	public GetAllMovieDataResponse getAllMovieData(
			GetAllMovieData getAllMovieData) throws GetAllMovieDataFault{
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public GetMovieInfoByGenreResponse getMovieInfoByGenre(
			GetMovieInfoByGenre getMovieInfoByGenre)
			throws GetMovieInfoByGenreFault {
		// TODO Auto-generated method stub
		return null;
	}

	

	@Override
	public GetMovieInfoByTheatreResponse getMovieInfoByTheatre(
			GetMovieInfoByTheatre getMovieInfoByTheatre)
			throws GetMovieInfoByTheatreFault {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GetMovieInfoByNameResponse getMovieInfoByName(
			GetMovieInfoByName getMovieInfoByName)
			throws GetMovieInfoByNameFault {

		// TODO Auto-generated method stub
		return null;
	}
	
	
	//converting functions here...
	
	public Event convertEvents(org.eventexample.www.events.Event eventArg)
	{
		Event event = new Event();
		event.setEvent_date_time(eventArg.getEvent_date_time());
		event.setEvent_description(eventArg.getEvent_description());
		event.setEvent_id(eventArg.getEvent_id());
		event.setEvent_link(eventArg.getEvent_link());
		event.setEvent_start_date(eventArg.getEvent_start_date());
		event.setEvent_title(eventArg.getEvent_title());
		
		return event;
	}
	
	public EventsArray convertGlobalEvents(GlobalEvents[] eventArg)
	{
		EventsArray facadeEventArray = new EventsArray();
		for(int i =0;i<eventArg.length;i++)
		{
			Event event = new Event();
			event.setEvent_date_time(eventArg[i].getEvent_date_time());
			event.setEvent_description(eventArg[i].getEvent_desctiption());
			event.setEvent_id(eventArg[i].getEvent_Id());
			event.setEvent_link(eventArg[i].getEvent_link());
			event.setEvent_start_date(eventArg[i].getEvent_start_date());
			event.setEvent_title(eventArg[i].getEvent_title());
			
			facadeEventArray.addEvent(event);
		}
		return facadeEventArray;
	}
	
	public News convertNews(org.newsexample.www.news.News newsArg)
	{
		News news = new News();
		news.setNews_id(newsArg.getNews_id());
		news.setNews_link(newsArg.getNews_link());
		news.setNews_title(newsArg.getNews_title());
		news.setNews_description(newsArg.getNews_description());
		news.setNews_category(newsArg.getNews_category());
		news.setNews_author(newsArg.getNews_author());
		news.setNews_publication_date(newsArg.getNews_publication_date());
		
		return news;
	}

	public RestaurantList convertRestaurantLists(org.sjsusearch.www.restaurantservice.RestaurantList resArg)
	{
		RestaurantList result = new RestaurantList();
		
		org.sjsusearch.www.restaurantservice.Restaurant[] resA = resArg.getRestaurantList();
		Restaurant[] restaurant = new Restaurant[resA.length];
		
		
		for(int i=0;i<resA.length;i++)
		{
			restaurant[i] = new Restaurant();
			restaurant[i].setRestaurant_id(resA[i].getRestaurant_id());
			restaurant[i].setRestaurant_latitude(resA[i].getRestaurant_latitude());
			restaurant[i].setRestaurant_location(resA[i].getRestaurant_location());
			restaurant[i].setRestaurant_longitude(resA[i].getRestaurant_longitude());
			restaurant[i].setRestaurant_name(resA[i].getRestaurant_name());
			restaurant[i].setRestaurant_rating(resA[i].getRestaurant_rating());
		}
		
		result.setRestaurantList(restaurant);
		return result;
	}
	public Restaurant convertRestaurant(org.sjsusearch.www.restaurantservice.Restaurant resA)
	{
		Restaurant restaurant = new Restaurant();
		
		restaurant = new Restaurant();
		restaurant.setRestaurant_id(resA.getRestaurant_id());
		restaurant.setRestaurant_latitude(resA.getRestaurant_latitude());
		restaurant.setRestaurant_location(resA.getRestaurant_location());
		restaurant.setRestaurant_longitude(resA.getRestaurant_longitude());
		restaurant.setRestaurant_name(resA.getRestaurant_name());
		restaurant.setRestaurant_rating(resA.getRestaurant_rating());
		
		return restaurant;
	}
	
	
	@Override
	public GetAllGlobalFacadeDataResponse getAllGlobalData(GetAllGlobalData getAllGlobalData)throws GetAllGlobalDataFaultException
	{
		//Calling Movie Service
		GetMovieByTime param1 = new GetMovieByTime();
		param1.setIn("4:30 PM");
		GetMovieByTimeResponse res = null;
		try {
			res = mStub.getMovieByTime(param1);
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (GetMovieByTimeFaultException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		GetAllGlobalFacadeDataResponse response = new GetAllGlobalFacadeDataResponse();
		GetAllData getAllDataParam = new GetAllData();
		getAllDataParam.setIn("");
		GetAllDataResponse globalResponse = new GetAllDataResponse();
		try {
			globalResponse = gStub.getAllData(getAllDataParam);
			GetAllGlobalFacadeData param = new GetAllGlobalFacadeData();
			EventsArray facadeEventArray = new EventsArray();
			//Convert from global Event Response to Facade Event Response
			facadeEventArray = this.convertGlobalEvents(globalResponse.getOut().getEventsData().getEvents());
			
			param.setEventsData(facadeEventArray);
			//param.setEventsData()
			response.setOut(param);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GetAllDataFaultException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}

}
